﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StockFinder.Model;

namespace StockFinder.Indicators.Day.HighPrice
{
    public class DonchianUpperChannelHighPriceDayIndicator : BaseDayIndicator
    {
        public DonchianUpperChannelHighPriceDayIndicator(int lookbackPeriod, string indicatorName) : 
            base(lookbackPeriod, indicatorName)
        {}

        public override void ApplyIndicator(List<DailyPrice> prices)
        {
            if (!Initialize(prices)) return;

            var orderedPrices = prices.OrderByDescending(p => p.PriceDate);
            DailyPrice currentPrice;

            var pricesArray = orderedPrices.ToArray();
            IEnumerable<DailyPrice> skippedPrices;
            IEnumerable<DailyPrice> takenPrices;
            var pricesArrayCount = pricesArray.Count();

            //array

            for (var i = 0; i < (pricesArrayCount - 1); i++)
            {
                currentPrice = pricesArray[i];

                skippedPrices = pricesArray.Skip(i+1);

                takenPrices = skippedPrices.Take(LookbackPeriod);

                currentPrice.DayIndicators[IndicatorName] = takenPrices.Max(p => p.AdjustedHigh);
            }
        }
    }
}
